/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.escuela.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.escuela.bl.MateriaBlLocal;
import mx.its.dsw2.escuela.modelo.Materia;

/**
 *
 * @author Liilie
 */
@ManagedBean
@SessionScoped
public class MatterBean {

    @EJB
    private MateriaBlLocal materiaBl;
    private List<Materia> lista;
    private Materia matter;
    private String accion;

    public MatterBean() {
        matter = new Materia();
    }

    public Materia getMatter() {
        return matter;
    }

    public void setMatter(Materia matter) {
        this.matter = matter;
    }

    public List<Materia> getLista() {
        lista = materiaBl.getAll();
        return lista;
    }

    //Acciones
    public String procesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            materiaBl.register(matter);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                materiaBl.modify(matter);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    materiaBl.delete(matter);
                }
            }
        }
        accion = "";
        return "MatterList";
    }

    public String procesarLlamada() {
        String page = "";
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            page = "MatterCreateEdit";
        } else {
            if (accion.equalsIgnoreCase("Eliminar")) {
                page = "MatterList";
            }
        }
        return page;
    }

//Listeners
    public void prepararNuevo(ActionEvent evt) {
        accion = "Nuevo";
        matter = new Materia();
    }

    public void prepararEditar(ActionEvent evt) {
        accion = "Editar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        matter = lista.get(index);
    }

    public void prepararEliminar(ActionEvent evt) {
        accion = "Eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        matter = lista.get(index);
    }
}
